package com.carambatv.android.adapter;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.carambatv.android.R;
import com.carambatv.android.db.ChannelQuery;
import com.carambatv.android.ui.CarambaApp;
import com.google.android.imageloader.ImageLoader;

public class GridAdapter extends CursorAdapter {

	private LayoutInflater mInflater;
	// private ImageLoaderNew imLoader;
	private CarambaApp app;
	private final ImageLoader mImageLoader;

	public GridAdapter(Context context, Cursor c) {
		super(context, c, false);
		mImageLoader = ImageLoader.get(context);
		// imLoader = new ImageLoaderNew(context);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		app = (CarambaApp) context.getApplicationContext();
	}

	@Override
	public void changeCursor(Cursor cursor) {
		super.changeCursor(cursor);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final YTubeViewHolder listItem = (YTubeViewHolder) view.getTag();
		final String title = getCursor().getString(ChannelQuery.TITLE);
		final String thumbnail = getCursor().getString(ChannelQuery.THUMBNAIL);
		final String id_channel = getCursor().getString(ChannelQuery.ID_CHANNEL);
		final String subscribers = getCursor().getString(ChannelQuery.SUBSCRIBERS);
		final String total = getCursor().getString(ChannelQuery.TOTAL);

		// imLoader.displayImage(thumbnail, (Activity) context, listItem.image);
		mImageLoader.bind(this, listItem.image, thumbnail);
		listItem.title.setText(title);
		listItem.title.setTypeface(app.getTypefaceHelvetica());
		listItem.subscribers.setText(context.getString(R.string.subscribers) + " " + subscribers);
		listItem.subscribers.setTypeface(app.getTypefaceHelvetica());
		listItem.subscribers.setTag(id_channel);
		listItem.total.setText(context.getString(R.string.total_count) + " " + total);
		listItem.total.setTypeface(app.getTypefaceHelvetica());
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.grid_item, null);
		final YTubeViewHolder listItem = new YTubeViewHolder();
		listItem.title = (TextView) view.findViewById(R.id.title);
		listItem.image = (ImageView) view.findViewById(R.id.image_video);
		listItem.subscribers = (TextView) view.findViewById(R.id.subscribers);
		listItem.total = (TextView) view.findViewById(R.id.total_count);

		view.setTag(listItem);
		return view;
	}

	public class YTubeViewHolder {
		TextView title;
		ImageView image;
		TextView subscribers;
		TextView total;
	}

}
